import Shop.damain.Goods;
import Shop.mappers.GoodsMapper;
import Shop.mappers.UsersMapper;
import Shop.service.GoodService;
import Shop.tools.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created by BadHuge on 2020/9/25.
 */
public class ShowGoods extends HttpServlet {
    int page =0;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        GoodService gs = new GoodService();
        HttpSession session = request.getSession();
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        request.setCharacterEncoding("utf-8");
      /*  int count = gs.QueryGoodCounts();*/
        int count = goodsMapper.queryGoodCounts();
        String flag=request.getParameter("flag");
        String gname =request.getParameter("gname");
/*        if (gname==null||gname.equals(null)||gname==""||gname.equals("")){

        }*/
        if(gname!=null&&!gname.equals("")){
            page =0;
        }
        if(flag!=null){
        int k = Integer.parseInt(flag.toString());
        if (k==0&&page>=5){
            page=page-5;
        }else if(k==1&&page<=(count-5)){
            System.out.print("wwwwwwwww"+(count-5));
            page=page+5;
        }}
        //Users myUser = (Users)session.getAttribute("user");
        //调用业务逻辑
        System.out.print("qqqqqqq"+page);
        List<Goods> goodslist = goodsMapper.queryGoodsByPage(gname,page);
        //共享对象
        session.setAttribute("mygoods",goodslist);
        //重定向
        response.sendRedirect("index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
